package com.shop.cereshop.commons.tencent.im.enums.notice;

import com.shop.cereshop.commons.tencent.im.model.dto.TargetTypeDTO;
import lombok.Getter;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

/**
 * @description: 通知目标类型枚举
 * @author zdd
 * @date 2025/9/15 11:27
 * @version 1.0
 */
@Getter
public enum SysNoticeTargetTypeEnum {

    VIDEO(1, "video", "视频"),
    POSTS(2, "posts", "动态"),
    COMMENT(3, "comment", "评论"),
    SHOP(4, "shop", "店铺"),
    PRODUCT(5, "product", "商品"),

    ;

    private final Integer code;
    private final String name;
    private final String msg;

    // 构造函数中添加基础校验
    private SysNoticeTargetTypeEnum(Integer code, String name, String msg) {
        if (code == null || name == null || msg == null) {
            throw new IllegalArgumentException("枚举参数不能为空");
        }
        this.code = code;
        this.name = name;
        this.msg = msg;
    }

    // 缓存 code -> Enum 映射
    private static final Map<Integer, SysNoticeTargetTypeEnum> CODE_MAP = new ConcurrentHashMap<>();

    static {
        for (SysNoticeTargetTypeEnum value : values()) {
            CODE_MAP.put(value.code, value);
        }
    }

    // 懒加载 DTO 列表
    private static class DtoHolder {
        private static final List<TargetTypeDTO> TARGET_TYPE_LIST =
                Arrays.stream(values())
                        .map(enumValue -> new TargetTypeDTO(
                                enumValue.getCode(),
                                enumValue.getName(),
                                enumValue.getMsg()
                        ))
                        .collect(Collectors.toList());
    }

    public static List<TargetTypeDTO> getTargetTypeList() {
        return DtoHolder.TARGET_TYPE_LIST;
    }

    public static String getNameByCode(Integer code) {
        if (Objects.isNull(code)) {
            return "";
        }
        SysNoticeTargetTypeEnum sysNoticeTargetTypeEnum = CODE_MAP.get(code);
        return sysNoticeTargetTypeEnum != null ? sysNoticeTargetTypeEnum.getName() : "";
    }

    public static String getMsgByCode(Integer code) {
        if (Objects.isNull(code)) {
            return "";
        }
        SysNoticeTargetTypeEnum sysNoticeTargetTypeEnum = CODE_MAP.get(code);
        return sysNoticeTargetTypeEnum != null ? sysNoticeTargetTypeEnum.getMsg() : "";
    }

}
